﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Business
{
    public partial class frm_Add_IQLastTimeRemain : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;

        public frm_Add_IQLastTimeRemain()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.inputDate.EditValue = DateTime.Now.Date;
            LoadIQList();
        }

        ////////////////////////////
        /// Get IQ ID List
        ////////////////////////////
        private void LoadIQList()
        {
            this.iqID.Properties.Items.Clear();
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT IQID FROM [222_IQ]"
                    + " WHERE IQID NOT IN (SELECT IQID FROM [241_IQLastTimeRemain])"
                    + " ORDER BY IQID";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["IQID"].ToString();
                    item.Value = dataReader["IQID"].ToString();
                    this.iqID.Properties.Items.Add(item);
                }
                dataReader.Close();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }                
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string error = null;
            DateTime inputdate = new DateTime();
            string iqid = null;
            int remain = 0;

            if (this.inputDate.EditValue == null)
            {
                error = "Please select input Date";
            }
            else
            {
                inputdate = DateTime.Parse(this.inputDate.EditValue.ToString());
            }

            if (string.IsNullOrEmpty(error))
            {
                if (this.iqID.SelectedIndex < 0)
                {
                    error = "Please select IQ ID";
                }
                else
                {
                    ComboboxItem item = (ComboboxItem)this.iqID.SelectedItem;
                    iqid = item.Value.ToString();
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                if (!Int32.TryParse(this.remainQuantity.Text, out remain))
                {
                    error = "Invalid Remain Quantity";
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                try
                {
                    myConnection.Open();
                    string sql = "INSERT INTO [241_IQLastTimeRemain] (LastDate, IQID, Remain)"
                            + "VALUES (@inputdate, '" + iqid + "', " + remain + ")";
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                    cmd_sql.Parameters.Add("@inputdate", SqlDbType.DateTime).Value = inputdate.Date;
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    MessageBox.Show("IQ Last Time Remain added success!");
                    clearForm();
                }
                catch
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    MessageBox.Show("Cannot add IQ Last Time Remain. Please try again!");
                }
            }
            else
            {
                MessageBox.Show(error);
            }
        }

        private void clearForm()
        {
            foreach (Control c in this.Controls)
            {
                if (c is DevExpress.XtraEditors.TextEdit)
                {
                    ((DevExpress.XtraEditors.TextEdit)c).Text = null;
                }

                if (c is DevExpress.XtraEditors.ComboBoxEdit)
                {
                    ((DevExpress.XtraEditors.ComboBoxEdit)c).SelectedIndex = -1;
                }

                if (c is System.Windows.Forms.MaskedTextBox)
                {
                    ((System.Windows.Forms.MaskedTextBox)c).Text = null;
                }
            }
            this.inputDate.EditValue = DateTime.Now.Date;
            LoadIQList();
        }
    }
}

